<template>
  <div class="list-wrap">
    <router-view/>
    <el-form class="form">
      <el-form-item label-width="100px" label="姓名：">
        <el-input v-model="username" type="text"/>
      </el-form-item>
      <el-form-item label-width="120px" label="手机号：">
        <el-input v-model="phone" type="text"/>
      </el-form-item>
      <el-form-item label-width="100px" label="邀请人：">
        <el-select v-model="recommendid" placeholder="请选择">
          <el-option
            v-for="item in infos"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label-width="140px" label="邀请人手机号：">
        <el-input v-model="recommendphone" type="text"/>
      </el-form-item>
      <el-form-item label-width="100px" label="会员等级：">
        <el-select v-model="userlevel" placeholder="请选择">
          <el-option
            v-for="item in vips"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label-width="100px" label="状态：">
        <el-select v-model="state" placeholder="请选择">
          <el-option
            v-for="item in states"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label-width="100px" label="注册时间：">
        <el-date-picker
          v-model="pickDate"
          :picker-options="pickerOptions2"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="left"/>
      </el-form-item>
      <el-form-item label-width="50px">
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" :summary-method="getSummaries" style="width: 100%;cursor: pointer;" border show-summary>
      <el-table-column prop="index" label="序号" width="180"/>
      <el-table-column prop="username" label="姓名" width="200"/>
      <el-table-column prop="phone" label="手机号" width="200"/>
      <el-table-column prop="userlevel" label="等级" width="200">
        <template slot-scope="scope">
          <div>{{ scope.row.userlevel === 1 ? '普通会员' : scope.row.userlevel === 2 ? '服务商' : '区域服务商' }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="registdate" sortable label="注册时间" width="200"/>
      <el-table-column prop="recommendphone" label="邀请人" width="200"/>
      <el-table-column prop="cansendcount" label="年卡" width="100"/>
      <el-table-column prop="totalcost" sortable label="消费金额" width="200"/>
      <el-table-column prop="totalrevenue" sortable label="收益总额" width="200"/>
      <el-table-column prop="servicefee" sortable label="服务费" width="200"/>
      <el-table-column prop="managementfee" sortable label="管理费" width="200"/>
      <el-table-column prop="withdraw" sortable label="累计提现" width="200"/>
      <el-table-column prop="balance" sortable label="余额" width="200"/>
      <el-table-column prop="fans" sortable label="粉丝" width="200"/>
      <el-table-column prop="state" label="状态" width="220">
        <template slot-scope="scope">
          <el-tag :type="scope.row.state === 1 ? 'success' : 'danger'">{{ scope.row.state === 1 ? '正常': '禁用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="ideisauth" label="个人实名认证" width="220">
        <template slot-scope="scope">
          <el-tag type="info">{{ scope.row.ideisauth === 1 ? '未认证' : '已认证' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="entisauth" label="企业实名认证" width="220">
        <template slot-scope="scope">
          <el-tag type="info">{{ scope.row.entisauth === 1 ? '未认证' : '已认证' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="switch" label="操作" width="200">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.leading"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="正常"
            inactive-text="禁用"
            class="switch-btn"
            @change="handleChangeState(scope.row.userid, scope.row.leading)"/>
          <el-button type="primary" @click="handleCheck(scope)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>10" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { userFind, userStop } from '@/api/user'
import { time } from '@/utils/index.js'
export default {
  name: 'Publish',
  components: {
    Pagination
  },
  data() {
    return {
      phone: '',
      infos: [
        {
          value: '',
          label: '全部'
        },
        {
          value: false,
          label: '无'
        },
        {
          value: true,
          label: '有'
        }
      ],
      vips: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 1,
          label: '普通'
        },
        {
          value: 2,
          label: '服务商'
        },
        {
          value: 3,
          label: '区域服务商'
        },
        {
          value: 4,
          label: '代理商'
        }
      ],
      states: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 1,
          label: '正常'
        },
        {
          value: 2,
          label: '禁用'
        }
      ],
      pickerOptions2: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      pickDate: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 10
      },
      list: [],
      centerDialogVisible: false,
      state: '',
      userlevel: '',
      iDisplayStart: 0,
      iDisplayLength: 10,
      username: '',
      recommendid: '',
      recommendphone: '',
      fromtime: '',
      totime: '',
      isagency: '',
      withdrawamount: '',
      totalrevenueamount: '',
      serviceamount: '',
      managementamount: '',
      totalcostamount: '',
      balanceamount: '',
      userid: ''
    }
  },
  computed: {
    tableData() {
      const result = []
      this.list.map((item, index) => {
        // const idx = this.type.findIndex((subitem) => {
        //   return item.typecode === subitem.code
        // })
        const flag = item.state === 1
        item = Object.assign({}, item, { index: `${(this.listQuery.page - 1) * this.listQuery.limit + 1 + index}`, edit: false, leading: flag })
        result.push(item)
      })
      return result
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    handleCheck(scope) {
      const id = scope.row.userid
      console.log(scope.row.userid)
      this.$router.push({
        path: `/vip/list/detail/${id}`,
        query: {
          props: JSON.stringify(scope.row)
        }
      })
    },
    handleDetail(params) {
      this.$router.push({
        path: '/vip/list/detail/3'
      })
    },
    getList(prop) {
      this.listQuery.page = prop.page
      this.listQuery.limit = prop.limit
      this.iDisplayStart = (prop.page - 1) * prop.limit
      this.iDisplayLength = prop.limit
      this.getUserList()
    },
    getSummaries(params) {
      const { columns } = params
      const sums = []
      const SUM = {
        text: '合计',
        index: 0,
        price: 'withdraw',
        earnings: 'totalrevenue',
        service: 'servicefee',
        management: 'managementfee',
        total: 'totalcost',
        balance: 'balance'
      }
      columns.map((item, index) => {
        if (index === SUM.index) {
          sums[index] = SUM.text
        }
        if (item.property === SUM.price) {
          sums[index] = this.withdrawamount
        }
        if (item.property === SUM.earnings) {
          sums[index] = this.totalrevenueamount
        }
        if (item.property === SUM.service) {
          sums[index] = this.serviceamount
        }
        if (item.property === SUM.management) {
          sums[index] = this.managementamount
        }
        if (item.property === SUM.total) {
          sums[index] = this.totalcostamount
        }
        if (item.property === SUM.balance) {
          sums[index] = this.balanceamount
        }
        sums.push('')
      })
      return sums
    },
    getUserList() {
      let fromtime, totime, isagency
      let { userlevel } = this
      const { phone, state, iDisplayStart, iDisplayLength, username, recommendid, recommendphone, pickDate } = this
      if (!pickDate.length) {
        fromtime = ''
        totime = ''
      } else {
        fromtime = `${time(pickDate[0])} ${new Date(pickDate[0]).toTimeString().substring(0, 8)}`
        totime = `${time(pickDate[1])} ${new Date(pickDate[1]).toTimeString().substring(0, 8)}`
      }
      if (Number(userlevel) === 4) {
        isagency = 1
        userlevel = ''
      } else {
        isagency = ''
      }
      userFind({ phone, state, userlevel, iDisplayStart, iDisplayLength, username, recommendid, recommendphone, fromtime, totime, isagency })
        .then((res) => {
          const data = res.data
          this.list = data
          this.total = res.total
          this.serviceamount = res.amount.serviceamount
          this.managementamount = res.amount.managementamount
          this.withdrawamount = res.amount.withdrawamount
          this.balanceamount = res.amount.balanceamount
          this.totalcostamount = res.amount.totalcostamount
          this.totalrevenueamount = res.amount.totalrevenueamount
        }).catch((e) => {
          this.$message.error({
            message: '请稍后再试',
            duration: 1000
          })
        })
    },
    handleSearch() {
      this.getUserList()
    },
    handleChangeState(userid, state) {
      state = state === false ? 2 : 1
      userStop(state, userid)
        .then((res) => {
          this.$message.success({
            message: '成功改变状态',
            duration: 1000
          })
          this.getUserList()
        }).catch((e) => {
          this.$message.error({
            message: '请稍后再试',
            duration: 1000
          })
        })
    }
  }
}
</script>
<style lang="scss" scoped>
.list-wrap {
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  .form {
    display: flex;
    flex-wrap: wrap;
  }
  .switch-btn {
    margin-top: 20px;
    margin-bottom: 20px;
    margin-left: 0px;
  }
}
</style>
